export function normalizePathKey(p: string): string {
  try {
    let s = String(p || '');
    // Trim quotes
    if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'"))) {
      s = s.slice(1, -1);
    }
    // Remove URL scheme
    s = s.replace(/^file:\/\/+/, '');
    // Unify slashes early for easier checks
    s = s.replace(/[\\\/]/g, '/');
    // Strip Windows extended-length prefix //?/
    if (s.startsWith('//?/')) {
      // Handle //?/UNC/server/share/... -> //server/share/...
      if (s.startsWith('//?/UNC/')) {
        s = '//' + s.slice('//?/UNC/'.length);
      } else {
        s = s.slice('//?/'.length);
      }
    }
    // Collapse duplicate slashes (but keep UNC leading double slash)
    s = s.replace(/([^:])\/+/g, (_m, g1) => g1 + '/');
    if (s.startsWith('///')) s = '/' + s.replace(/^\/+/, '//');
    // Normalize drive letter casing: upper-case
    s = s.replace(/^([a-zA-Z]):\//, (_m, d: string) => d.toUpperCase() + ':/');
    return s;
  } catch {
    return String(p || '');
  }
}